from django.shortcuts import render, HttpResponse, redirect
from tools import cos
from apps.web import models

def setting(request, project_id):
    return render(request, 'web/setting.html')


def project_delete(request, project_id):
    """删除项目"""
    # 拿到表单提交的数据
    project_name = request.POST.get('project_name')
    if not project_name or (project_name != request.tracer.project.name):
        return render(request, 'web/setting.html', {'error': '项目名错误'})
    # 项目名称正确
    # 判断是否为创建者
    if request.tracer.user != request.tracer.project.creator:
        return render(request, 'web/setting.html', {'error': '只有项目创建者有权限删除项目'})
    # 删除桶和数据库项目
    # 1.删除桶里面所有文件
    # 2.删除所有碎片文件
    # 3.删除桶
    # 4.删除数据库中项目
    cos.delete_bucket(request.tracer.project.bucket, request.tracer.project.region)
    models.Project.objects.filter(id=request.tracer.project.id).delete()
    return redirect('project_list')
